module.exports = function helper(proxys, defaultProxy) {
  let targetProxy = defaultProxy;
  try {
    // yarn
    const argvByYarn = process.argv || [];
    const findIndex = argvByYarn.findIndex((ele) => {
      return ele === "--proxy";
    });
    if (findIndex > -1) {
      const key = argvByYarn[findIndex + 1];
      targetProxy = proxys[key];
    }
    // npm
    const argvByNpm = JSON.parse(process.env.npm_config_argv).original;
    const regex = /^--proxy-([\w]+)$/;
    const find = argvByNpm.find((argv) => {
      return regex.test(argv);
    });
    if (find) {
      const [, key] = find.match(regex);
      targetProxy = proxys[key];
    }
    // eslint-disable-next-line no-empty
  } catch (error) {}

  return targetProxy;
};
